package com.oascarmovie.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oascarmovie.R;
import com.oascarmovie.model.CatalogModel;

public class CatalogWidget extends RelativeLayout {

  private final static int NORMAL_BACKGROUND = 0x00000000;
  private final static int FOCUSED_BACKGROUND = 0xFFFFFF00;

  private List<TextView> catalogSelectedTextView = new ArrayList<TextView>();

  public CatalogWidget(Context context, List<CatalogModel> catalogList,
      String header, int elementCountPerRow, boolean defaultFocusedOnAll) {
    super(context);

    LayoutInflater.from(context).inflate(R.layout.catalogwidget, this);
    TextView textView_Header = (TextView) findViewById(R.id.textview_catalog_header);
    textView_Header.setText(header);

    LinearLayout tableLayout = (LinearLayout) findViewById(R.id.table_catalog);
    LinearLayout firstRow = (LinearLayout) findViewById(R.id.catalog_row);
    TextView textView_All = (TextView) findViewById(R.id.textview_catalog_all);

    OnClickListener catalogTextViewClickListener = new OnClickListener() {
      @Override
      public void onClick(View v) {
        focusOnTextView((TextView) v, catalogSelectedTextView);
      }
    };

    buildCatalog(tableLayout, firstRow, textView_All,
        catalogTextViewClickListener, catalogList, elementCountPerRow);
    
    if (defaultFocusedOnAll) {
      focusOnTextView(textView_All, catalogSelectedTextView);
    }

  }

  private void focusOnTextView(TextView focusedTextView,
      List<TextView> catalogSelectedView) {
    for (TextView textView : catalogSelectedView) {
      String text = textView.getText().toString();
      Spannable str = new SpannableString(text);
      str.setSpan(new BackgroundColorSpan(NORMAL_BACKGROUND), 0, str.length(),
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      textView.setText(str);

    }

    String text = focusedTextView.getText().toString();
    Spannable str = new SpannableString(text);
    str.setSpan(new BackgroundColorSpan(FOCUSED_BACKGROUND), 0, str.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    focusedTextView.setText(str);
    catalogSelectedView.add(focusedTextView);
  }

  private void buildCatalog(LinearLayout tableLayout, LinearLayout firstRow,
      TextView textView_All, OnClickListener clickListener,
      List<CatalogModel> catalogList, int rowCount) {

    textView_All.setOnClickListener(clickListener);

    LinearLayout row = firstRow;

    int count = 0;
    for (CatalogModel catalogModel : catalogList) {

      if (count == rowCount || (count > rowCount && (count % rowCount == 0))) {
        row = new LinearLayout(this.getContext());
        row.setLayoutParams(firstRow.getLayoutParams());
        tableLayout.addView(row);
      }
      if (row != null) {
        TextView textView = new TextView(this.getContext());
        textView.setLayoutParams(textView_All.getLayoutParams());
        textView.setText(catalogModel.getName());
        textView.setOnClickListener(clickListener);
        row.addView(textView);
      }
      count++;
    }

  }

}
